<?php

namespace addons\shopro\controller;

use addons\shopro\model\RobOrderModel;

/**
 * 预约抢购
 * Class RobOrder
 * @package addons\shopro\controller
 */
class RobOrder extends Base
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = ['*'];

    public function index()
    {
        $robOrderModel = new RobOrderModel();
        $page          = input('param.page/d', 1);
        $rows          = input('param.rows/d', 20);
        $startTime     = input('param.start_time', '');
        $endTime       = input('param.end_time', '');
        $result        = $robOrderModel->getLists([
            'user_id'    => $this->auth->getUser()->id,
            'start_time' => $startTime,
            'end_time'   => $endTime
        ], $rows, $page, [
            'a.id',
            'a.title',
            'a.goods_id',
            'a.activity_id',
            'a.activity_price',
            'a.is_use',
            'a.is_rob',
            'a.create_time',
            'b.nickname',
            'b.avatar',
            'b.mobile',
            'c.title' => 'goods_title',
            'c.image'
        ]);

        $this->success('获取成功', $result);
    }

    /**
     * 立即预约
     * @return \think\response\Json
     */
    public function order()
    {
        $data = input('post.activity/a');

        if (empty($data['id']) || empty($data['goods_id']) || empty($data['status_code']) || empty($data['price'])) {
            return json([
                'code' => 701,
                'msg'  => '参数错误'
            ]);
        }

        if ($data['status_code'] != 'ing') {
            return json([
                'code' => 702,
                'msg'  => '还未到预约时间'
            ]);
        }

        $res = RobOrderModel::add($this->auth->getUser(), $data);

        return json($res);
    }
}
